var moduleService = require('../../service/moduleService');
/**
 * 模块控制器 
 * Author: 梦里小白龙 <jiangwenshu1990@126.com> <http://www.jz07.cn>
 */

module.exports  = {
    //添加模块
    save ( req, res){
        let params = {
            pid:req.body.pid,
            title:req.body.title,
            icon:req.body.icon,
            module:req.body.module,
            name:req.body.name,
            status:req.body.status
        }

        //添加模块信息
        moduleService.save(params).then(data=>{
            data ? res.json({code:1 ,msg:'添加成功'}) : res.json({code:0 ,msg:'添加失败'});
        });
    },
    //更新数据
    update(req ,rea){
        let params = {
            pid:req.body.pid,
            title:req.body.title,
            txt:req.body.txt,
            icon:req.body.icon,
            module:req.body.module,
            name:req.body.name,
            status:req.body.status,
            id:req.body.id,
        }
        //添加角色信息
        moduleService.update(params).then(data=>{
            data ? res.json({code:1 ,msg:'添加成功'}) : res.json({code:0 ,msg:'添加失败'});
        });
    },
    //查询模块信息
    getById(req, res, next){
        if(!req.body.id){
            res.json({code:2004 ,msg:'参数错误'})
        } 
        //查询角色信息
        moduleService.getById(req.body.id).then(data=>{
            data ? res.json({code:1 ,msg:'查询成功',info: data}) : res.json({code:0 ,msg:'查询失败'});
        })
    },
    //删除角色数据
    delete(req, res, next){
        
        let moduleId = req.body.id;

        //查询pid是否包含该角色
        moduleService.getParentId(moduleId).then(response=>{
            if(response.length >= 1){
                res.json({code:-1 ,msg:'该模块下绑定其他模块'})
            }else{
                //删除角色信息
                moduleService.udelete(moduleId).then(data=>{
                    data ? res.json({code:1 ,msg:'删除成功'}) : res.json({code:0 ,msg:'删除失败'});
                })
            }
        })
    },
    //查询当前id下的模块
    parentModule(req, res, next){
        
        let moduleId = req.body.id;

        //查询pid是否包含该角色
        moduleService.getParentId(moduleId).then(data=>{
            data.length > 0 ? res.json({code:1 ,msg:'查询成功',lists:data}) : res.json({code:0 ,msg:'无任何数据'});
        })
    },
}